<template>
    <!-- 添加或修改设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false"
        v-dialogDrag>
        <el-form ref="formType3" :model="formType3" :rules="rulesType3" label-width="120px" v-if="info.type == 3">
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="维修保养类型" prop="type" required>
                        <el-select v-model="formType3.type" placeholder="请选择维修保养类型" clearable style="width: 130px">
                            <el-option v-for="dict in dict.type.shebei_type" :key="dict.value" :label="dict.label"
                                :value="dict.value" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="设备名称" prop="name" required>
                        <el-input v-model="formType3.name" placeholder="请输入设备名称" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="设备编码" prop="code" required>
                        <el-input v-model="formType3.code" placeholder="请输入设备编码" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="设备品牌" prop="pinpai">
                        <el-input v-model="formType3.pinpai" placeholder="请输入设备品牌" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="型号" prop="xinghao">
                        <el-input v-model="formType3.xinghao" placeholder="请输入型号" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="故障问题" prop="problem">
                        <el-input v-model="formType3.problem" placeholder="请输入故障问题" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="维修/保养内容" prop="maintenance">
                        <el-input v-model="formType3.maintenance" placeholder="请输入维修/保养内容" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="维修/保养时间" prop="date">
                        <el-date-picker clearable v-model="formType3.date" value-format="yyyy-MM-dd HH:mm:ss"
                            type="datetime" placeholder="请选择维修/保养时间">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="维修/维护人员" prop="userName">
                        <el-input v-model="formType3.userName" placeholder="请输入维修/维护人员" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="维修保养质量验收记录" prop="checkContent">
                        <el-input v-model="formType3.checkContent" placeholder="维修保养质量验收记录" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="验收人" prop="checkName">
                        <el-input v-model="formType3.checkName" placeholder="请输入验收人" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="维修/保养文件" prop="file">
                        <file-copy-upload v-model="formType3.file" />
                    </el-form-item>
                </el-col>

                <el-col :span="12">
                    <el-form-item label="其他文件" prop="otherFile">
                        <file-copy-upload v-model="formType3.otherFile" />
                    </el-form-item>
                </el-col>

                <el-col :span="24">
                    <el-form-item label="备注" prop="remark">
                        <el-input v-model="formType3.remark" placeholder="请输入备注" />
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <el-tabs v-model="activeName" class="detail-tab" type="card" v-if="info.type == 1">
            <el-tab-pane label="车间维修" name="second">
                <el-form ref="formType1" :model="formType1" :rules="rulesType1" label-width="120px"
                    v-if="info.type == 1">
                    <el-row :gutter="20">
                        <el-col :span="12">
                            <el-form-item label="维修保养类型" prop="type">
                                <el-select v-model="formType1.type" placeholder="请选择维修保养类型" clearable
                                    style="width: 130px">
                                    <el-option v-for="dict in dict.type.shebei_type" :key="dict.value"
                                        :label="dict.label" :value="dict.value" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="设备编号" prop="code">
                                <el-input v-model="formType1.code" placeholder="请输入设备编号" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="设备名称" prop="name">
                                <el-input v-model="formType1.name" placeholder="请输入设备名称" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="设备型号" prop="xinghao">
                                <el-input v-model="formType1.xinghao" placeholder="请输入设备型号" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="使用部门" prop="haveHouse">
                                <el-input v-model="formType1.haveHouse" placeholder="请输入使用部门" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="检修申请人" prop="jianxiuApply">
                                <el-input v-model="formType1.jianxiuApply" placeholder="请输入检修申请人" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="故障问题" prop="problem">
                                <el-input v-model="formType1.problem" placeholder="请输入故障问题" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="故障类别" prop="problemType">
                                <el-select v-model="formType1.problemType" placeholder="请选择故障类别" clearable
                                    style="width: 130px">
                                    <el-option v-for="dict in dict.type.problem_type" :key="dict.value"
                                        :label="dict.label" :value="dict.value" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="维修/养内容" prop="maintenance">
                                <el-input v-model="formType1.maintenance" placeholder="请输入维修/养内容" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="维修/保养金额" prop="money">
                                <el-input v-model="formType1.money" placeholder="请输入维修/保养金额" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="维修/保养时间" prop="date">
                                <el-date-picker clearable v-model="formType1.date" value-format="yyyy-MM-dd HH:mm:ss"
                                    type="datetime" placeholder="请选择维修/保养时间">
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="维修/保养人" prop="userName">
                                <el-input v-model="formType1.userName" placeholder="请输入维修/保养人" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="维修保养验收记录" prop="checkContent">
                                <el-input v-model="formType1.checkContent" placeholder="请输入维修保养验收记录" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="验收人" prop="checkName">
                                <el-input v-model="formType1.checkName" placeholder="请输入验收人" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="检修记录" prop="jianxiuRecord">
                                <el-input v-model="formType1.jianxiuRecord" placeholder="请输入检修记录" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="检修人" prop="jianxiuName">
                                <el-input v-model="formType1.jianxiuName" placeholder="请输入检修人" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="检修时间" prop="jianxiuDate">
                                <el-date-picker clearable v-model="formType1.jianxiuDate"
                                    value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="请选择检修时间">
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="维修/保养文件" prop="file">
                                <file-copy-upload v-model="formType1.file" />
                            </el-form-item>
                        </el-col>

                        <el-col :span="12">
                            <el-form-item label="其他文件" prop="otherFile">
                                <file-copy-upload v-model="formType1.otherFile" />
                            </el-form-item>
                        </el-col>

                    </el-row>
                </el-form>
            </el-tab-pane>
            <el-tab-pane label="车间保养" name="third">
                <el-form ref="formType4" :model="formType4" :rules="rulesType4" label-width="120px">
                    <el-row :gutter="20">
                        <el-col :span="12">
                            <el-form-item label="维修保养类型" prop="type">
                                <el-select v-model="formType4.type" placeholder="请选择维修保养类型" clearable
                                    style="width: 130px">
                                    <el-option v-for="dict in dict.type.shebei_type" :key="dict.value"
                                        :label="dict.label" :value="dict.value" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="设备编号" prop="code">
                                <el-input v-model="formType4.code" placeholder="请输入设备编号" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="设备名称" prop="name">
                                <el-input v-model="formType4.name" placeholder="请输入设备名称" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="设备型号" prop="xinghao">
                                <el-input v-model="formType4.xinghao" placeholder="请输入设备型号" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="生产部门" prop="haveHouse">
                                <el-input v-model="formType4.haveHouse" placeholder="请输入生产部门" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="点检内容" prop="maintenance">
                                <el-input v-model="formType4.maintenance" placeholder="请输入点检内容" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="点检人" prop="userName">
                                <el-input v-model="formType4.userName" placeholder="请输入点检人" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="维修/保养文件" prop="file">
                                <file-copy-upload v-model="formType4.file" />
                            </el-form-item>
                        </el-col>

                        <el-col :span="12">
                            <el-form-item label="其他文件" prop="otherFile">
                                <file-copy-upload v-model="formType4.otherFile" />
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </el-tab-pane>
        </el-tabs>
        <el-form ref="formType2" :model="formType2" :rules="rulesType2" label-width="120px" v-if="info.type == 2">
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item label="维修保养类型" prop="type">
                        <el-select v-model="formType2.type" placeholder="请选择维修保养类型" clearable style="width: 130px">
                            <el-option v-for="dict in dict.type.shebei_type" :key="dict.value" :label="dict.label"
                                :value="dict.value" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="设备编号" prop="code">
                        <el-input v-model="formType2.code" placeholder="请输入设备编号" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="设备名称" prop="name">
                        <el-input v-model="formType2.name" placeholder="请输入设备名称" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="设备型号" prop="xinghao">
                        <el-input v-model="formType2.xinghao" placeholder="请输入设备型号" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="使用部门" prop="haveHouse">
                        <el-input v-model="formType2.haveHouse" placeholder="请输入使用部门" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="检修申请人" prop="jianxiuApply">
                        <el-input v-model="formType2.jianxiuApply" placeholder="请输入检修申请人" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="故障问题" prop="problem">
                        <el-input v-model="formType2.problem" placeholder="请输入故障问题" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="故障类别" prop="problemType">
                        <el-select v-model="formType2.problemType" placeholder="请选择故障类别" clearable style="width: 130px">
                            <el-option v-for="dict in dict.type.problem_type" :key="dict.value" :label="dict.label"
                                :value="dict.value" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="维修/养内容" prop="maintenance">
                        <el-input v-model="formType2.maintenance" placeholder="请输入维修/养内容" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="维修/保养金额" prop="money">
                        <el-input v-model="formType2.money" placeholder="请输入维修/保养金额" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="维修/保养时间" prop="date">
                        <el-date-picker clearable v-model="formType2.date" value-format="yyyy-MM-dd HH:mm:ss"
                            type="datetime" placeholder="请选择维修/保养时间">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="维修/保养人" prop="userName">
                        <el-input v-model="formType2.userName" placeholder="请输入维修/保养人" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="维修保养验收记录" prop="checkContent">
                        <el-input v-model="formType2.checkContent" placeholder="请输入维修保养验收记录" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="验收人" prop="checkName">
                        <el-input v-model="formType2.checkName" placeholder="请输入验收人" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="检修记录" prop="jianxiuRecord">
                        <el-input v-model="formType2.jianxiuRecord" placeholder="请输入检修记录" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="检修人" prop="jianxiuName">
                        <el-input v-model="formType2.jianxiuName" placeholder="请输入检修人" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="检修时间" prop="jianxiuDate">
                        <el-date-picker clearable v-model="formType2.jianxiuDate" value-format="yyyy-MM-dd HH:mm:ss"
                            type="datetime" placeholder="请选择检修时间">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="维修/保养文件" prop="file">
                        <file-copy-upload v-model="formType2.file" />
                    </el-form-item>
                </el-col>

                <el-col :span="12">
                    <el-form-item label="其他文件" prop="otherFile">
                        <file-copy-upload v-model="formType2.otherFile" />
                    </el-form-item>
                </el-col>

            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { addAction, getById, updateAction } from '@/api/manage';
export default {
    name: "maintenanceForm",
    dicts: ['shebei_type', 'problem_type'],
    data() {
        return {
            id: '',
            editId: '',
            formType1: {},
            formType2: {},
            formType3: {},
            formType4: {},
            info: {},
            // 遮罩层
            loading: true,
            activeName: "second",
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 表单参数
            form: {},
            url: {
                getById: '/lnShebeiWeibao/lnShebeiWeibao/',
                add: '/lnShebeiWeibao/lnShebeiWeibao/add',
                edit: '/lnShebeiWeibao/lnShebeiWeibao/edit',
            },
            // 表单校验
            rulesType1: {
                repairType: [
                    { required: true, message: '请输入维修保养类型', trigger: 'blur' }
                ],
                code: [
                    { required: true, message: '请输入设备编号', trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '请输入设备名称', trigger: 'blur' }
                ],
                xinghao: [
                    { required: true, message: '请输入设备型号', trigger: 'blur' }
                ],
                director: [
                    { required: true, message: '请输入车间主任', trigger: 'blur' }
                ],
            },
            rulesType2: {
                replacementParts: [
                    { required: true, message: '请输入维修保养类型', trigger: 'blur' }
                ],
                code: [
                    { required: true, message: '请输入设备编号', trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '请输入设备名称', trigger: 'blur' }
                ],
                maintenanceTime: [
                    { required: true, message: '请输入设备型号', trigger: 'blur' }
                ],
                haveHouse: [
                    { required: true, message: '请输入使用部门', trigger: 'blur' }
                ],
                money: [
                    { required: true, message: '请输入维修/保养金额', trigger: 'blur' }
                ],
                date: [
                    { required: true, message: '请输入维修/保养时间', trigger: 'blur' }
                ],
                userName: [
                    { required: true, message: '请输入维修/保养人', trigger: 'blur' }
                ],
                checkName: [
                    { required: true, message: '请输入验收人', trigger: 'blur' }
                ],
                jianxiuDate: [
                    { required: true, message: '请输入检修时间', trigger: 'blur' }
                ]
            },
            rulesType3: {
                supplier: [
                    { required: true, message: '请输入维修保养类型', trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '请输入设备名称', trigger: 'blur' }
                ],
                code: [
                    { required: true, message: '请输入设备编码', trigger: 'blur' }
                ],
                pinpai: [
                    { required: true, message: '请输入设备品牌', trigger: 'blur' }
                ],
                xinghao: [
                    { required: true, message: '请输入型号', trigger: 'blur' }
                ],
                userName: [
                    { required: true, message: '请输入维修/维护人员', trigger: 'blur' }
                ],
                checkName: [
                    { required: true, message: '请输入验收人', trigger: 'blur' }
                ]
            },
            rulesType4: {
                maintenanceContent: [
                    { required: true, message: '请输入维修保养类型', trigger: 'blur' }
                ],
                code: [
                    { required: true, message: '请输入设备编号', trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '请输入设备名称', trigger: 'blur' }
                ],
                xinghao: [
                    { required: true, message: '请输入设备型号', trigger: 'blur' }
                ],
                userName: [
                    { required: true, message: '请输入点检人', trigger: 'blur' }
                ]
            },
        };

    },
    created() {

    },
    methods: {
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            // 重置表单数据
            this.formType1 = {};
            this.formType2 = {};
            this.formType3 = {};
            this.formType4 = {};

            // 重置表单验证
            if (this.$refs.formType1) {
                this.$refs.formType1.resetFields();
            }
            if (this.$refs.formType2) {
                this.$refs.formType2.resetFields();
            }
            if (this.$refs.formType3) {
                this.$refs.formType3.resetFields();
            }
            if (this.$refs.formType4) {
                this.$refs.formType4.resetFields();
            }
        },
        /** 新增按钮操作 */
        handleAdd(info) {
            this.reset();
            this.info = info;
            this.open = true;
            this.id = info.id;
            this.formType1.type = info.type;
            this.formType1.name = info.name;
            this.formType1.code = info.code;
            this.formType1.pinpai = info.pinpai;
            this.formType1.xinghao = info.xinghao;
            this.formType2.type = info.type;
            this.formType2.name = info.name;
            this.formType2.code = info.code;
            this.formType2.pinpai = info.pinpai;
            this.formType2.xinghao = info.xinghao;
            this.formType3.type = info.type;
            this.formType3.name = info.name;
            this.formType3.code = info.code;
            this.formType3.pinpai = info.pinpai;
            this.formType3.xinghao = info.xinghao;
            this.formType4.type = info.type;
            this.formType4.name = info.name;
            this.formType4.code = info.code;
            this.formType4.pinpai = info.pinpai;
            this.formType4.xinghao = info.xinghao;
            this.editId = '';
            this.title = "添加记录";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            this.editId = row.id;
            this.info = row;

            getById(this.url.getById, id).then(response => {
                // 根据类型设置对应表单的数据
                this.formType1 = response.data;
                this.formType4 = response.data;
                this.formType2 = response.data;
                this.formType3 = response.data;
                this.open = true;
                this.title = "修改记录";
            });
        },
        /** 提交按钮 */
        submitForm() {
            let validFormRef = null;
            let validRules = null;
            let validForm = null;

            if (this.info.type == 1) {
                // 对于type==1，需要根据当前激活的标签页来决定验证哪个表单
                if (this.activeName === 'second') {
                    validFormRef = "formType1";
                    validRules = this.rulesType1;
                    validForm = this.formType1;
                } else if (this.activeName === 'third') {
                    validFormRef = "formType4";
                    validRules = this.rulesType4;
                    validForm = this.formType4;
                }
            } else if (this.info.type == 2) {
                validFormRef = "formType2";
                validRules = this.rulesType2;
                validForm = this.formType2;
            } else if (this.info.type == 3) {
                validFormRef = "formType3";
                validRules = this.rulesType3;
                validForm = this.formType3;
            }

            if (validFormRef) {
                this.$refs[validFormRef].validate(valid => {
                    // if (valid) {
                    //     validForm.parentId = this.info.id;
                    //     addAction(this.url.add, validForm).then(response => {
                    //         this.$modal.msgSuccess("新增成功");
                    //         this.open = false;
                    //         this.$emit('ok');
                    //     });
                    // }

                    if (this.editId) {
                        updateAction(this.url.edit, validForm).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.$emit('ok');
                        });
                    } else {
                        validForm.parentId = this.id;
                        addAction(this.url.add, validForm).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.$emit('ok');
                        });
                    }
                });
            }
        }
    }
}
</script>
